package ch.zbw.pra.sysventory.presentation.panels;

import java.util.Iterator;

import ch.zbw.pra.sysventory.application.controller.UserController;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.VerticalLayout;

/**
 * Painted das NavigationPanel
 * 
 * @author Ralph Koster
 */

public class NavigationPanel extends VerticalLayout implements Button.ClickListener {

	private static final long serialVersionUID = -2256210381775358966L;
	private UserController userController;
	private ApplicationPanel applicationPanel;
	private CssLayout navigationPanel;
	private VerticalLayout titlePanel;
	private VerticalLayout menuButtonPanel;
	private VerticalLayout fillSpacePanel;
	private HorizontalLayout bottomButtonPanel;
	private Label titleLogo;
	private Label titleText;
	private Button logoutButton;
	private Button settingsButton;
	
	
	public NavigationPanel(ApplicationPanel applicationPanel) {
		this.applicationPanel = applicationPanel;
		this.userController = UserController.getInstance();
		this.navigationPanel = new CssLayout();
		this.titlePanel = new VerticalLayout();
		this.menuButtonPanel = new VerticalLayout();
		this.fillSpacePanel = new VerticalLayout();
		this.bottomButtonPanel = new HorizontalLayout();
		this.titleLogo = new Label();
		this.titleText = new Label("Sysventory");
		this.logoutButton = new NativeButton();
		this.settingsButton = new NativeButton();

		paint();
	}
	
	public void paint() {
		this.addStyleName("navigation-panel");
		this.setSizeFull();
		this.setWidth("130px");
		this.setMargin(false);
//		this.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		
		titleLogo.setIcon(new ThemeResource("img/logo.png"));
		titleLogo.setStyleName("title-logo");
		titleText.setStyleName("title-panel-text-h2");
		titlePanel.setStyleName("title-panel");
		titlePanel.setWidth("130px");
		titlePanel.setHeight("130px");
		titlePanel.addComponent(titleLogo);
		titlePanel.addComponent(titleText);
		
		for (final String view : new String[] { "home", "scan", "archiv", "statistik"}) {
			Button button = new NativeButton(view.substring(0, 1).toUpperCase()
	                + view.substring(1).replace('-', ' '));
	        button.addStyleName("button-" + view);
	        button.setWidth("130px");
	        button.setHeight("70px");
	        button.addListener(this);
		
			if("scan".equals(view) && userController.hasPermission()) {
				menuButtonPanel.addComponent(button);
			}
			
			if(!"scan".equals(view)) {
				if("home".equals(view)) {
					button.addStyleName("selected");
				}
				menuButtonPanel.addComponent(button);
			}
		}
		
		menuButtonPanel.setSizeUndefined();
		menuButtonPanel.setStyleName("menuButton-panel");
		fillSpacePanel.setStyleName("fillSpace-panel");
		bottomButtonPanel.setStyleName("bottomButton-panel");
		
		fillSpacePanel.setHeight("120px");
		
		logoutButton.setStyleName("button-logout");
		logoutButton.addListener(this);
		settingsButton.setStyleName("button-settings");
		bottomButtonPanel.setHeight("30px");
		
		logoutButton.setWidth("65px");
		logoutButton.setHeight("30px");
		logoutButton.setDescription("Logout");
		
		settingsButton.setWidth("65px");
		settingsButton.setHeight("30px");
		settingsButton.setDescription("Button für Einstellungen. Im Moment nicht implementiert");
		
		bottomButtonPanel.addComponent(logoutButton);
		bottomButtonPanel.addComponent(settingsButton);
		
		navigationPanel.addComponent(titlePanel);
		navigationPanel.addComponent(menuButtonPanel);
		navigationPanel.addComponent(fillSpacePanel);
		navigationPanel.addComponent(bottomButtonPanel);

		this.addComponent(navigationPanel);
	}

	@Override
	public void buttonClick(ClickEvent event) {
		clearMenuSelection();
		if(event.getButton() == logoutButton) {
			delegateLogout();
		} else {
			String buttonStyleName = event.getButton().getStyleName();
			event.getButton().addStyleName("selected");
			navigateView(buttonStyleName);
		}
	}

	private void clearMenuSelection() {
		 for (Iterator<Component> it = menuButtonPanel.getComponentIterator(); it.hasNext();) {
	            Component next = it.next();
	            if (next instanceof NativeButton) {
	                next.removeStyleName("selected");
	            }
		 }
	}
	
	private void navigateView(String buttonStyleName) {
		applicationPanel.changeView(buttonStyleName);
	}
	
	private void delegateLogout() {
		applicationPanel.logout();
	}
}